import { Request, Response } from 'express';
import operationalService from '../../services/operational.service';
export class Controller {
    // 获取运营位接口
    get(req: Request, res: Response): void {
        let status = Number(req.query.status as string);
        if (!status) {
            status = 0;
        }
        operationalService.getOperational(status)
        .then((data) => {
            res.status(200).json({ msg: 'ok', datas: data });
        })
        .catch((error) => {
            res.status(200).json({ msg: 'error', error: error });
        });
    }
    // 新增运营位接口
    add(req: Request, res: Response): void {
        // 解析请求参数
        const { name, status, cover, content, uri } = req.body;
        operationalService.addOperational({ name, status, cover, content, uri })
        .then(() => {
            res.status(200).json({ msg: 'ok' });
        })
        .catch((error) => {
            res.status(200).json({ msg: 'error', error: error });
        });
    }
    // 更新运营位状态接口
    update(req: Request, res: Response): void {
        let { id, status } = req.body;
        if (status !== 0 && status !== 1) {
            status = 0;
        }
        if (!id) {
            res.status(200).json({ msg: 'error', error: '请指定要更新的运营位 id' });
            return;
        }
        operationalService.updateOperational(status, id)
        .then(() => {
            res.status(200).json({ msg: 'ok' });
        })
        .catch((error) => {
            res.status(200).json({ msg: 'error', error: error });
        });
    }
}
export default new Controller();